#!/bin/sh

TEST_DIR=''

cleanup () {
  case "${TEST_DIR}" in
    /*) rm -rf "${TEST_DIR}" ;;
  esac
  unset -f nvm_ls_remote_index_tab run_ensure reset_state assert_no_calls assert_one_call
}

die () { echo "$@" ; cleanup ; exit 1; }

: nvm.sh
\. ../../../nvm.sh

TEST_DIR="$(mktemp -d)" || die 'could not create a temp dir'
NVM_DIR="${TEST_DIR}/nvm"
export NVM_DIR
mkdir -p "${NVM_DIR}" || die 'could not create NVM_DIR'
CALLS="${TEST_DIR}/calls"
STATUS_PATH="${NVM_DIR}/.lts-status"

unset NVM_OFFLINE NVM_INDEX_NO_FETCH NVM_LTS

# MOCK_CREATE=1 records LTS status; MOCK_EXIT is the mock's exit code
nvm_ls_remote_index_tab () {
  printf '%s|[%s][%s][%s]|NVM_LTS=[%s]\n' "$#" "${1-}" "${2-}" "${3-}" "${NVM_LTS-unset}" >> "${CALLS}"
  echo 'mock stdout'
  echo 'mock stderr' >&2
  if [ "${MOCK_CREATE-}" = 1 ]; then
    echo 'v24.11.0 krypton' > "${STATUS_PATH}"
  fi
  return "${MOCK_EXIT-0}"
}

# args: expected exit code, description
run_ensure () {
  OUTPUT="$(nvm_ensure_lts_status 2>&1)" && EXIT_CODE=0 || EXIT_CODE=$?
  [ "_${EXIT_CODE}" = "_$1" ] || die "$2: exit code >${EXIT_CODE}<, expected >$1<"
  [ "_${OUTPUT}" = '_' ] || die "$2: output >${OUTPUT}<, expected nothing"
}

reset_state () {
  case "${TEST_DIR}" in
    /*) rm -rf "${STATUS_PATH}" "${CALLS}" ;;
  esac
}

assert_no_calls () {
  [ ! -f "${CALLS}" ] || die "$1: should not fetch the index; got >$(cat "${CALLS}")<"
}

assert_one_call () {
  [ -f "${CALLS}" ] || die "$1: should fetch the index"
  GOT="$(cat "${CALLS}")"
  EXPECTED='3|[node][std][]|NVM_LTS=[]'
  [ "_${GOT}" = "_${EXPECTED}" ] || die "$1: index fetch was >${GOT}<, expected >${EXPECTED}<"
}

# offline or told not to fetch, without a record
reset_state
MOCK_CREATE=1
NVM_OFFLINE=1
run_ensure 3 'NVM_OFFLINE=1 without a record'
assert_no_calls 'NVM_OFFLINE=1 without a record'
[ ! -e "${STATUS_PATH}" ] || die 'NVM_OFFLINE=1 should not record LTS status'

unset NVM_OFFLINE
for FLAG in 1 no ' '; do
  NVM_INDEX_NO_FETCH="${FLAG}"
  run_ensure 3 "NVM_INDEX_NO_FETCH=>${FLAG}< without a record"
  assert_no_calls "NVM_INDEX_NO_FETCH=>${FLAG}< without a record"
done

NVM_OFFLINE=1
NVM_INDEX_NO_FETCH=1
run_ensure 3 'both offline flags without a record'
assert_no_calls 'both offline flags without a record'

# a directory is not a record
mkdir "${STATUS_PATH}" || die 'could not create a directory at the status path'
unset NVM_INDEX_NO_FETCH
run_ensure 3 'a directory at the status path while offline'
assert_no_calls 'a directory at the status path while offline'
unset NVM_OFFLINE
MOCK_CREATE=0
run_ensure 1 'a directory at the status path that a fetch cannot replace'
assert_one_call 'a directory at the status path'
rmdir "${STATUS_PATH}" || die 'could not remove the directory at the status path'

# an existing record needs no fetch, even when fetching is allowed
reset_state
echo 'v24.11.0 krypton' > "${STATUS_PATH}"
run_ensure 0 'an existing record'
assert_no_calls 'an existing record'
NVM_OFFLINE=1
run_ensure 0 'an existing record while offline'
unset NVM_OFFLINE
NVM_INDEX_NO_FETCH=1
run_ensure 0 'an existing record with NVM_INDEX_NO_FETCH=1'
assert_no_calls 'an existing record with offline flags'

# the record follows NVM_DIR
NVM_DIR="${TEST_DIR}/other"
run_ensure 3 'a record in another NVM_DIR'
NVM_DIR="${TEST_DIR}/nvm"
unset NVM_INDEX_NO_FETCH

reset_state
: > "${STATUS_PATH}"
run_ensure 0 'an empty record'
assert_no_calls 'an empty record'

reset_state
echo 'v24.11.0 krypton' > "${TEST_DIR}/real-status"
ln -s "${TEST_DIR}/real-status" "${STATUS_PATH}" || die 'could not symlink the status path'
run_ensure 0 'a symlinked record'
assert_no_calls 'a symlinked record'

# fetching records status; NVM_LTS is cleared for the fetch and output is silenced
NVM_LTS=argon
export NVM_LTS
NVM_INDEX_NO_FETCH=''
for FLAG in '' 0 true ' 1'; do
  reset_state
  MOCK_CREATE=1
  MOCK_EXIT=0
  NVM_OFFLINE="${FLAG}"
  run_ensure 0 "a fetch that records status (NVM_OFFLINE=>${FLAG}<)"
  assert_one_call "a fetch that records status (NVM_OFFLINE=>${FLAG}<)"
  [ -f "${STATUS_PATH}" ] || die 'the fetch should have recorded status'
done
unset NVM_OFFLINE

# called directly, so a change to the caller's NVM_LTS would show
reset_state
nvm_ensure_lts_status >/dev/null 2>&1 || die 'a direct call with a fetch that records status should succeed'
assert_one_call 'a direct call'
[ "_${NVM_LTS-unset}" = '_argon' ] || die "the caller's NVM_LTS was >${NVM_LTS-unset}<, expected >argon<"
unset NVM_INDEX_NO_FETCH NVM_LTS

# success depends on the record, not on the fetch's exit code
reset_state
MOCK_CREATE=1
MOCK_EXIT=2
run_ensure 0 'a failing fetch that still records status'
assert_one_call 'a failing fetch that still records status'

reset_state
MOCK_CREATE=0
MOCK_EXIT=0
run_ensure 1 'a successful fetch that records nothing'
assert_one_call 'a successful fetch that records nothing'

reset_state
MOCK_EXIT=4
run_ensure 1 'a failing fetch that records nothing'
assert_one_call 'a failing fetch that records nothing'

cleanup
